package com.jumi.microservice.service.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.common.constant.ImageResizeEnum;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.DateUtil;
import com.jumi.microservice.common.utils.EnumUtil;
import com.jumi.microservice.common.utils.ImageResizeUtil;
import com.jumi.microservice.config.ExpireTimeConfig;
import com.jumi.microservice.constant.compensation.AfterSaleEnum;
import com.jumi.microservice.constant.compensation.AfterSaleStatusEnum;
import com.jumi.microservice.constant.compensation.CheckStatusEnum;
import com.jumi.microservice.constant.refundgoods.*;
import com.jumi.microservice.domain.GoodsSkuInfoDTO;
import com.jumi.microservice.dto.order.OrderLogRequest;
import com.jumi.microservice.dto.refundgoods.*;
import com.jumi.microservice.dto.serviceDto.*;
import com.jumi.microservice.entity.Compensation;
import com.jumi.microservice.entity.RefundGoods;
import com.jumi.microservice.enumerate.LogisticsStatusEnum;
import com.jumi.microservice.enumerate.OrderStatusEnum;
import com.jumi.microservice.enumerate.SelfFlagEnum;
import com.jumi.microservice.exception.CompensationExceptionEnum;
import com.jumi.microservice.exception.RefundGoodsExceptionEnum;
import com.jumi.microservice.mapper.CompensationMapper;
import com.jumi.microservice.mapper.RefundGoodsMapper;
import com.jumi.microservice.service.CommodityApi;
import com.jumi.microservice.service.IOrderDubboService;
import com.jumi.microservice.service.IOrderService;
import com.jumi.microservice.service.admin.RefundGoodsCommonService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author Scw
 * @Date 2020-08-19 10:12
 */
@Service
public class RefundGoodsService {
    @Resource
    private RefundGoodsMapper refundGoodsMapper;
    @DubboReference
    private IOrderDubboService iOrderDubboService;
    @DubboReference
    private IOrderService iOrderService;
    @DubboReference
    private CommodityApi goodsClient;
    @Resource
    private CompensationMapper compensationMapper;
    @Resource
    private RefundGoodsCommonService refundGoodsCommonService;
    @Resource
    private ExpireTimeConfig expireTimeConfig;

    /**
     * 商品出库中弹窗提示接口
     */
    public String refundDelivery(Integer orderId, String skuIdStr) {
        String refundDelivery = "";
        boolean isDelivery = iOrderService.isDelivery(orderId, skuIdStr);
        if (isDelivery) {
            refundDelivery = RefundGoodsExceptionEnum.GOODS_NO_SHIP.getMessage();
        }
        return refundDelivery;
    }

    /**
     * 退货添加
     *
     * @param refundGoodsAddRequest
     * @return
     */
    @org.springframework.transaction.annotation.Transactional
    public Boolean addRefundGoods(RefundGoodsAddRequest refundGoodsAddRequest) {
        Long operatorId = refundGoodsAddRequest.getOperatorId();
        String operatorName = refundGoodsAddRequest.getOperatorName();

        /*
         * 根据订单ID查询订单状态【订单表】
         * 订单状态：0-已取消、1-待付款、2-待发货、3-待收货、4-已完成
         * 发起退货：3-待收货、4-已完成【待收货8天以内】
         * 补发：3-待收货、4-已完成
         * 补偿：3-待收货、4-已完成
         * 补邮：3-待收货、4-已完成
         * 换货：3-待收货、4-已完成
         * 补发漏发：3-待收货、4-已完成
         *
         */
        //根据订单ID查询订单状态【订单表】
        Integer orderStatus = iOrderService.getOrderStatus(refundGoodsAddRequest.getOriginalOrderId());
        if (orderStatus != null) {
            //Integer[] notAllowArr = {0, 1, 2};
            Integer[] notAllowArr = {
                    OrderStatusEnum.CANCEL.getCode().intValue(),
                    OrderStatusEnum.READY_TO_PAY.getCode().intValue(),
                    OrderStatusEnum.READY_TO_SHIP.getCode().intValue()
            };
            if (Arrays.asList(notAllowArr).contains(orderStatus)) {
                throw new BaseException(RefundGoodsExceptionEnum.ORDER_STATUS_NOT_ALLOW.getCode(), RefundGoodsExceptionEnum.ORDER_STATUS_NOT_ALLOW.getMessage());
            }
            //订单状态：3-待收货、4-已完成
            if (orderStatus == OrderStatusEnum.READY_TO_RECEIVE.getCode().intValue() || orderStatus == OrderStatusEnum.COMPLETED.getCode().intValue()) {
                //收货时间
                Date confirmTime = iOrderService.getConfirmTime(refundGoodsAddRequest.getOriginalOrderId());
                if (confirmTime != null) {
                    //收货时间 + 8 < 当前时间
                    if (DateUtil.getDateAfterDay(confirmTime, Integer.parseInt(expireTimeConfig.getDiffDayOne())).before(new Date())) {
                        throw new BaseException(RefundGoodsExceptionEnum.ORDER_OVER_TIME.getCode(), RefundGoodsExceptionEnum.ORDER_OVER_TIME.getMessage());
                    }
                }
            }
        }
        /*
         * 根据订单ID/商品ID查询 商品状态、商品实发数量、售后状态、退货数量/补发数量【订单商品表】
         * 商品物流状态 -1 取消订单 0 待发货 1 部分发货 2 待收货 3 已完成
         * 发起退货：1 部分发货 2 待收货 3 已完成
         */
        OrderStatusAfterSealResponse orderStatusAfterSealResponse = iOrderService.getStatusAfterSeal(refundGoodsAddRequest.getOriginalOrderId(), refundGoodsAddRequest.getRefundGoodsId());
        if (orderStatusAfterSealResponse != null) {
            int goodsNum = orderStatusAfterSealResponse.getRealSendAmount();
            int returnNum = orderStatusAfterSealResponse.getReturnCount();
            int refundNum = refundGoodsAddRequest.getRefundNum();
            int totalNum = returnNum + refundNum;
            int goodsStatus = orderStatusAfterSealResponse.getGoodsStatus().intValue();
            //Integer[] notAllowArr = {-1, 0};
            Integer[] notAllowArr2 = {
                    LogisticsStatusEnum.CANCEL.getCode().intValue(),
                    LogisticsStatusEnum.WAIT_SEND.getCode().intValue()
            };
            if (Arrays.asList(notAllowArr2).contains(goodsStatus)) {
                throw new BaseException(RefundGoodsExceptionEnum.GOODS_NO_SHIP.getCode(), RefundGoodsExceptionEnum.GOODS_NO_SHIP.getMessage());
            }
            if (totalNum > goodsNum) {
                throw new BaseException(RefundGoodsExceptionEnum.GOODS_STATUS_MAX_NUM.getCode(), RefundGoodsExceptionEnum.GOODS_STATUS_MAX_NUM.getMessage());
            }
            /*
             * 售后状态:
             *      0-正常 1-已退款
             *      2-退货审核中 3-已退货退款 4-退货退款拒绝
             *      5-补发审核中 6-已补发 7-补发拒绝
             *      8-补偿审核中 9-已补偿 10-补偿拒绝
             *      11-补邮审核中 12-已补邮费 13-补邮拒绝
             *      14-换货申请中 15-换货同意  16-换货拒绝
             *      17-补发漏发审核中 18-已补发漏发 19-补发漏发拒绝
             * 发起退货：0-正常、4-退货退款拒绝、7-补发拒绝、10-补偿拒绝、16-换货拒绝、19-补发漏发拒绝
             * 补发：0-正常、4-退货退款拒绝、7-补发拒绝、10-补偿拒绝、16-换货拒绝、19-补发漏发拒绝
             * 补偿：0-正常、4-退货退款拒绝、7-补发拒绝、10-补偿拒绝、16-换货拒绝、19-补发漏发拒绝
             * 补邮：2-退货审核中、3-已退货退款、13-补邮拒绝、14-换货申请中、15-换货同意
             * 换货：0-正常、4-退货退款拒绝、7-补发拒绝、10-补偿拒绝、16-换货拒绝、19-补发漏发拒绝
             * 补发漏发：0-正常、4-退货退款拒绝、7-补发拒绝、10-补偿拒绝、16-换货拒绝、19-补发漏发拒绝
             */
            //Integer[] notAfterSaleArr = {1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15，17，18};
            Integer[] notAfterSaleArr = {
                    AfterSaleStatusEnum.REFUND.getCode(),
                    AfterSaleStatusEnum.REFUND_GOODS_APPLY.getCode(),
                    AfterSaleStatusEnum.REFUND_GOODS_AGREE.getCode(),
                    AfterSaleStatusEnum.REISSUE_APPLY.getCode(),
                    AfterSaleStatusEnum.REISSUE_AGREE.getCode(),
                    AfterSaleStatusEnum.COMPENSATION_APPLY.getCode(),
                    AfterSaleStatusEnum.COMPENSATION_AGREE.getCode(),
                    AfterSaleStatusEnum.COMPENSATION_POSTAGE_APPLY.getCode(),
                    AfterSaleStatusEnum.COMPENSATION_POSTAGE_AGREE.getCode(),
                    AfterSaleStatusEnum.COMPENSATION_POSTAGE_REFUSE.getCode(),
                    AfterSaleStatusEnum.REPLACE_APPLY.getCode(),
                    AfterSaleStatusEnum.REPLACE_AGREE.getCode(),
                    AfterSaleStatusEnum.REISSUE_MISSED_APPLY.getCode(),
                    AfterSaleStatusEnum.REISSUE_MISSED_AGREE.getCode()
            };
            Integer afterSaleStatus = orderStatusAfterSealResponse.getAfterSealStatus().intValue();
            if (Arrays.asList(notAfterSaleArr).contains(afterSaleStatus)) {
                throw new BaseException(RefundGoodsExceptionEnum.GOODS_STATUS_NOT_ALLOW.getCode(), RefundGoodsExceptionEnum.GOODS_STATUS_NOT_ALLOW.getMessage());
            }
        }
        boolean res = false;
        RefundGoods refundGoods = new RefundGoods();
        BeanUtils.copyProperties(refundGoodsAddRequest, refundGoods);
        /*
         * 前台退货退款，不退邮费，退货退款邮费为0
         */
        refundGoods.setRefundPostage(BigDecimal.valueOf(0));
        //refundGoods.setRefundTotalPrice(refundGoodsAddRequest.getRefundPrice());
        //refundGoods.setRefundTotalPrice(refundGoodsAddRequest.getRefundPrice().add(refundGoodsAddRequest.getRefundPostage()));
        refundGoods.setRefundOrderNo(this.generateOrderSn());
        refundGoods.setRefundPromoterId(operatorId);
        refundGoods.setRefundPromoterName(operatorName);
        //根据订单ID和商品ID查询原支付订单信息
        OrderGoodsServiceDetailsRequest orderGoodsServiceDetailsRequest = new OrderGoodsServiceDetailsRequest();
        orderGoodsServiceDetailsRequest.setOrderId(refundGoodsAddRequest.getOriginalOrderId());
        orderGoodsServiceDetailsRequest.setGoodsId(refundGoodsAddRequest.getRefundGoodsId());
        OrderGoodsServiceDetailsResponse orderGoodsServiceDetailsResponse = iOrderService.findOrderGoodsDetails(orderGoodsServiceDetailsRequest);
        if (orderGoodsServiceDetailsResponse == null) {
            throw new BaseException(500, "订单信息有误！");
        }
        refundGoods.setRefundTotalPrice(orderGoodsServiceDetailsResponse.getGoodsPrice().multiply(BigDecimal.valueOf(refundGoodsAddRequest.getRefundNum())));
        refundGoods.setSelfFlag(orderGoodsServiceDetailsResponse.getSelfFlag());
        refundGoods.setOrderCreateTime(orderGoodsServiceDetailsResponse.getCreateTime());
        if(orderGoodsServiceDetailsResponse.getSelfFlag().equals(SelfFlagEnum.YES_SELF.getCode())){
            refundGoods.setSupplierId(orderGoodsServiceDetailsResponse.getRealWarehouse());
        }else{
            refundGoods.setSupplierId(orderGoodsServiceDetailsResponse.getSupplierId());
        }
        refundGoods.setRefundSpuId(orderGoodsServiceDetailsResponse.getSpuId());
        refundGoods.setGoodsName(orderGoodsServiceDetailsResponse.getGoodsName());
        refundGoods.setGoodsPic(orderGoodsServiceDetailsResponse.getGoodsCoverImg());
        refundGoods.setGoodsSpec(orderGoodsServiceDetailsResponse.getGoodsSpec());
        refundGoods.setGoodsAmount(orderGoodsServiceDetailsResponse.getGoodsAmount());
        refundGoods.setOrderPayNo(orderGoodsServiceDetailsResponse.getPayNo());
        refundGoods.setOrderPayType(orderGoodsServiceDetailsResponse.getPayType());
        refundGoods.setOrderThirdPartyPaySn(orderGoodsServiceDetailsResponse.getThirdPartyPaySn());
        refundGoods.setOrderPayTime(orderGoodsServiceDetailsResponse.getPayTime());
        refundGoods.setIsGroup(orderGoodsServiceDetailsResponse.getIsGroup());
        if (refundGoodsMapper.insert(refundGoods) > 0) {
            /*
             * 订单商品表更新售后状态
             * 售后状态:
             *      0-正常 1-已退款
             *      2-退货审核中 3-已退货退款 4-退货退款拒绝
             *      5-补发审核中 6-已补发 7-补发拒绝
             *      8-补偿审核中 9-已补偿 10-补偿拒绝
             *      11-补邮审核中 12-已补邮费 13-补邮拒绝
             *      14-换货申请中 15-换货同意  16-换货拒绝
             *      17-补发漏发审核中 18-已补发漏发 19-补发漏发拒绝
             * after_seal_status = 2
             */
            //int afterSealStatus = 2;
            int afterSealStatus = AfterSaleStatusEnum.REFUND_GOODS_APPLY.getCode();
            UpdateRefundGoodsInfoRequest updateRefundGoodsInfoRequest = new UpdateRefundGoodsInfoRequest();
            updateRefundGoodsInfoRequest.setOrderId(refundGoodsAddRequest.getOriginalOrderId());
            updateRefundGoodsInfoRequest.setGoodsId(refundGoodsAddRequest.getRefundGoodsId());
            updateRefundGoodsInfoRequest.setAfterSealStatus((byte) afterSealStatus);
            updateRefundGoodsInfoRequest.setRefundId(refundGoods.getId());
            Boolean isStatus = iOrderService.updateRefundGoodsInfo(updateRefundGoodsInfoRequest);

            /*
             * 操作日志表插入相关信息
             */
            String logInfo = "发起退货退款：" + refundGoods.getGoodsName() + refundGoods.getRefundNum();
            operatorName = "买家";
            //用户标识，1 前端用户， 2 后端管理员
            Byte userFlag = CancelUserFlagEnum.USER_FLAG_CUSTOMER.getCode().byteValue();
            OrderLogRequest orderLogRequest = new OrderLogRequest();
            orderLogRequest.setLog(logInfo);
            orderLogRequest.setOperatorId(operatorId);
            orderLogRequest.setOperatorName(operatorName);
            orderLogRequest.setUserFlag(userFlag);
            orderLogRequest.setOrderSn(refundGoodsAddRequest.getOriginalOrderNo());
            Boolean isAddLog = iOrderDubboService.addOrderLog(orderLogRequest);
            if (isStatus && isAddLog) {
                res = true;
            }
        }
        return res;
    }

    /**
     * 物流信息添加
     *
     * @param refundGoodsLogisticsRequest
     * @param refundGoodsRequest
     * @return
     */
    @org.springframework.transaction.annotation.Transactional
    public Boolean addRefundGoodsLogistics(RefundGoodsLogisticsRequest refundGoodsLogisticsRequest, RefundGoodsRequest refundGoodsRequest) {
        Long operatorId = refundGoodsLogisticsRequest.getOperatorId();
        String operatorName = refundGoodsLogisticsRequest.getOperatorName();

        boolean res = false;
        int refundId = refundGoodsRequest.getRefundId();
        //查询信息
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", refundId);
        queryWrapper.eq("buyer_id", refundGoodsRequest.getUid());
        queryWrapper.eq("status", RefundGoodsStatusEnum.STATUS_RECEIVED.getCode());
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        RefundGoods refundGoods = refundGoodsMapper.selectOne(queryWrapper);
        if (refundGoods == null) {
            throw new BaseException(RefundGoodsExceptionEnum.INFO_NO_EXIST.getCode(), RefundGoodsExceptionEnum.INFO_NO_EXIST.getMessage());
        }
        refundGoods.setLogisticsNo(refundGoodsLogisticsRequest.getLogisticsNo());
        refundGoods.setLogisticsCompany(refundGoodsLogisticsRequest.getLogisticsCompany());
        refundGoods.setExpressCode(refundGoodsLogisticsRequest.getExpressCode());
        if (!"".equals(refundGoodsLogisticsRequest.getLogisticsRemark())) {
            refundGoods.setLogisticsRemark(refundGoodsLogisticsRequest.getLogisticsRemark());
        }
        refundGoods.setLogisticsOperatorId(operatorId);
        refundGoods.setLogisticsOperatorName(operatorName);
        refundGoods.setLogisticsTime(new Date());
        String logInfo;
        if ("".equals(refundGoods.getLogisticsNo()) || null == refundGoods.getLogisticsNo()) {
            logInfo = "上传物流单号：" + refundGoodsLogisticsRequest.getLogisticsNo();
        } else {
            logInfo = "更新物流单号：" + refundGoodsLogisticsRequest.getLogisticsNo();
        }
        if (refundGoodsMapper.updateById(refundGoods) > 0) {
            /*
             * 操作日志表插入相关信息
             */
            operatorName = "买家";
            //用户标识，1 前端用户， 2 后端管理员
            Byte userFlag = CancelUserFlagEnum.USER_FLAG_CUSTOMER.getCode().byteValue();
            OrderLogRequest orderLogRequest = new OrderLogRequest();
            orderLogRequest.setLog(logInfo);
            orderLogRequest.setOperatorId(operatorId);
            orderLogRequest.setOperatorName(operatorName);
            orderLogRequest.setUserFlag(userFlag);
            orderLogRequest.setOrderSn(refundGoods.getOriginalOrderNo());
            Boolean isAdd = iOrderDubboService.addOrderLog(orderLogRequest);
            if (isAdd) {
                res = true;
            }
        }
        return res;
    }

    /**
     * 售后服务-删除记录
     *
     * @param refundGoodsDelRequest
     * @param refundGoodsRequest
     * @return
     */
    @org.springframework.transaction.annotation.Transactional
    public Boolean delRefundGoods(RefundGoodsDelRequest refundGoodsDelRequest, RefundGoodsRequest refundGoodsRequest) {
        Long operatorId = refundGoodsDelRequest.getOperatorId();
        String operatorName = refundGoodsDelRequest.getOperatorName();

        boolean res = false;
        int refundId = refundGoodsRequest.getRefundId();

        //查询信息
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", refundId);
        queryWrapper.eq("buyer_id", refundGoodsRequest.getUid());
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        RefundGoods refundGoods = refundGoodsMapper.selectOne(queryWrapper);
        if (refundGoods == null) {
            throw new BaseException(RefundGoodsExceptionEnum.INFO_NO_EXIST.getCode(), RefundGoodsExceptionEnum.INFO_NO_EXIST.getMessage());
        }
        refundGoods.setDelFlag(DelFlag.DELETE);
        refundGoods.setCancelOperatorId(operatorId);
        refundGoods.setCancelOperatorName(operatorName);
        refundGoods.setCancelTime(new Date());
        refundGoods.setCancelUserFlag(CancelUserFlagEnum.USER_FLAG_CUSTOMER.getCode().byteValue());
        if (refundGoodsMapper.updateById(refundGoods) > 0) {
            /*
             * 操作日志表插入相关信息
             */
            String logInfo = "售后处理-删除";
            if (refundGoods.getRefundType() == RefundTypeEnum.REFUND_TYPE_GOODS.getCode().byteValue()) {
                logInfo += "退货退款";
            } else {
                logInfo += "退款";
            }
            operatorName = "买家";
            //用户标识，1 前端用户， 2 后端管理员
            Byte userFlag = CancelUserFlagEnum.USER_FLAG_CUSTOMER.getCode().byteValue();
            OrderLogRequest orderLogRequest = new OrderLogRequest();
            orderLogRequest.setLog(logInfo);
            orderLogRequest.setOperatorId(operatorId);
            orderLogRequest.setOperatorName(operatorName);
            orderLogRequest.setUserFlag(userFlag);
            orderLogRequest.setOrderSn(refundGoods.getOriginalOrderNo());
            Boolean isAdd = iOrderDubboService.addOrderLog(orderLogRequest);
            if (isAdd) {
                res = true;
            }
        }
        return res;
    }

    /**
     * 退货取消
     *
     * @param refundGoodsStatusRequest
     * @param refundGoodsRequest
     * @return
     */
    @org.springframework.transaction.annotation.Transactional
    public Boolean cancelRefundGoods(RefundGoodsStatusRequest refundGoodsStatusRequest, RefundGoodsRequest refundGoodsRequest) {
        Long operatorId = refundGoodsStatusRequest.getOperatorId();
        String operatorName = refundGoodsStatusRequest.getOperatorName();
        operatorName = "买家";

        boolean res = false;
        int refundId = refundGoodsRequest.getRefundId();
        List<Integer> statusVals = new ArrayList<Integer>();
        statusVals.add(RefundGoodsStatusEnum.STATUS_DEAL_WITH.getCode());
        statusVals.add(RefundGoodsStatusEnum.STATUS_RECEIVED.getCode());
        statusVals.add(RefundGoodsStatusEnum.STATUS_CHECK.getCode());
        //查询信息
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", refundId);
        queryWrapper.eq("buyer_id", refundGoodsRequest.getUid());
        queryWrapper.in("status", statusVals);
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        RefundGoods refundGoods = refundGoodsMapper.selectOne(queryWrapper);
        if (refundGoods != null) {
            refundGoods.setStatus(RefundGoodsStatusEnum.STATUS_CANCEL.getCode().byteValue());
            refundGoods.setCancelOperatorId(operatorId);
            refundGoods.setCancelOperatorName(operatorName);
            refundGoods.setCancelTime(new Date());
            refundGoods.setCancelUserFlag(CancelUserFlagEnum.USER_FLAG_CUSTOMER.getCode().byteValue());
            if (refundGoodsMapper.updateById(refundGoods) > 0) {
                //弃货审核取消
                if (refundGoods.getRenounceFlag().intValue() == RenounceFlagEnum.RENOUNCE_YES.getCode()) {
                    //查询信息
                    QueryWrapper<Compensation> queryWrapperCompensation = new QueryWrapper<>();
                    queryWrapperCompensation.eq("refund_id", refundGoods.getId());
                    queryWrapperCompensation.eq("after_sale_type", AfterSaleEnum.AFTER_SALE_QHTK.getCode());
                    queryWrapperCompensation.eq("check_status", CheckStatusEnum.CHECK_STATUS_WAIT.getCode());
                    queryWrapperCompensation.eq("original_order_id", refundGoods.getOriginalOrderId());
                    queryWrapperCompensation.eq("compensation_goods_id", refundGoods.getRefundGoodsId());
                    Compensation compensation = compensationMapper.selectOne(queryWrapperCompensation);
                    if (compensation != null) {
                        compensation.setCheckStatus(CheckStatusEnum.CHECK_STATUS_REFUSE.getCode().byteValue());
                        compensation.setCheckRemark(operatorName);
                        compensation.setCheckOperatorId(operatorId);
                        compensation.setCheckOperatorName(operatorName);
                        compensation.setCheckTime(new Date());
                        compensationMapper.updateById(compensation);
                    }
                }
                /*
                 * 操作日志表插入相关信息
                 */
                String logInfo = "售后处理-取消";
                if (refundGoods.getRefundType() == RefundTypeEnum.REFUND_TYPE_GOODS.getCode().byteValue()) {
                    logInfo += "退货退款";
                } else {
                    logInfo += "退款";
                }
                //用户标识，1 前端用户， 2 后端管理员
                Byte userFlag = CancelUserFlagEnum.USER_FLAG_CUSTOMER.getCode().byteValue();
                OrderLogRequest orderLogRequest = new OrderLogRequest();
                orderLogRequest.setLog(logInfo);
                orderLogRequest.setOperatorId(operatorId);
                orderLogRequest.setOperatorName(operatorName);
                orderLogRequest.setUserFlag(userFlag);
                orderLogRequest.setOrderSn(refundGoods.getOriginalOrderNo());
                Boolean isAdd = iOrderDubboService.addOrderLog(orderLogRequest);
                /*
                 * 订单商品表更新售后状态
                 * 售后状态:
                 *      0-正常 1-已退款
                 *      2-退货审核中 3-已退货退款 4-退货退款拒绝
                 *      5-补发审核中 6-已补发 7-补发拒绝
                 *      8-补偿审核中 9-已补偿 10-补偿拒绝
                 *      11-补邮审核中 12-已补邮费 13-补邮拒绝
                 *      14-换货申请中 15-换货同意  16-换货拒绝
                 *      17-补发漏发审核中 18-已补发漏发 19-补发漏发拒绝
                 * after_seal_status = 0
                 */
                //int afterSealStatus = 0;
                int afterSealStatus = AfterSaleStatusEnum.NORMAL.getCode();
                UpdateAfterSealStatusRequest updateAfterSealStatusRequest = new UpdateAfterSealStatusRequest();
                updateAfterSealStatusRequest.setOrderId(refundGoods.getOriginalOrderId());
                updateAfterSealStatusRequest.setGoodsId(refundGoods.getRefundGoodsId());
                updateAfterSealStatusRequest.setAfterSealStatus((byte) afterSealStatus);
                Boolean isStatus = iOrderService.updateAfterSealStatus(updateAfterSealStatusRequest);
                if (isAdd && isStatus) {
                    res = true;
                }
            }
        }
        return res;
    }

    /**
     * 售后详情
     *
     * @param refundGoodsRequest
     * @return
     */
    public RefundGoodsInfoResponse refundGoodsDetail(RefundGoodsRequest refundGoodsRequest) {
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", refundGoodsRequest.getRefundId());
        queryWrapper.eq("buyer_id", refundGoodsRequest.getUid());
        RefundGoods refundGoods = refundGoodsMapper.selectOne(queryWrapper);
        if (refundGoods == null) {
            throw new BaseException(RefundGoodsExceptionEnum.INFO_NO_EXIST.getCode(), RefundGoodsExceptionEnum.INFO_NO_EXIST.getMessage());
        }
        //判断是否已删除
        if (refundGoods.getDelFlag() == DelFlag.DELETE) {
            throw new BaseException(RefundGoodsExceptionEnum.DEL_FLAG_YES.getCode(), RefundGoodsExceptionEnum.DEL_FLAG_YES.getMessage());
        }
        /*************************自动取消************************/
        RefundGoodsAutoCancelRequest refundGoodsAutoCancelRequest = new RefundGoodsAutoCancelRequest();
        refundGoodsAutoCancelRequest.setStatus(refundGoods.getStatus());
        refundGoodsAutoCancelRequest.setUid(refundGoods.getBuyerId());
        refundGoodsAutoCancelRequest.setCreateTime(refundGoods.getCreateTime());
        refundGoodsAutoCancelRequest.setDealWithTime(refundGoods.getDealWithTime());
        refundGoodsAutoCancelRequest.setLogisticsNo(refundGoods.getLogisticsNo());
        refundGoodsAutoCancelRequest.setLogisticsTime(refundGoods.getLogisticsTime());
        refundGoodsAutoCancelRequest.setRefundType(refundGoods.getRefundType());
        refundGoodsAutoCancelRequest.setRefundId(refundGoods.getId());
        refundGoodsAutoCancelRequest.setOrderId(refundGoods.getOriginalOrderId());
        refundGoodsAutoCancelRequest.setGoodsId(refundGoods.getRefundGoodsId());
        refundGoodsCommonService.autoCancel(refundGoodsAutoCancelRequest);
        /*************************自动取消************************/
        RefundGoodsInfoResponse refundGoodsInfoResponse = new RefundGoodsInfoResponse();
        BeanUtils.copyProperties(refundGoods, refundGoodsInfoResponse);
        refundGoodsInfoResponse.setRefundId(refundGoods.getId());
        //售后原因对照字典
        if (refundGoods.getRefundType() == RefundTypeEnum.REFUND_TYPE_GOODS.getCode().byteValue()) {
            //退货退款
            RefundReasonEnum refundReasonEnum = EnumUtil.getByCode(refundGoods.getRefundReason().intValue(), RefundReasonEnum.class);
            if (refundReasonEnum != null) {
                refundGoodsInfoResponse.setRefundReasonStr(refundReasonEnum.getDesc());
            }
        } else {
            //退款
            RefundMoneyReasonEnum refundMoneyReasonEnum = EnumUtil.getByCode(refundGoods.getRefundReason().intValue(), RefundMoneyReasonEnum.class);
            if (refundMoneyReasonEnum != null) {
                refundGoodsInfoResponse.setRefundReasonStr(refundMoneyReasonEnum.getDesc());
            }
        }
        if (refundGoods.getReceivedStatus() > 0) {
            //收货状态对照字典
            ReceivedStatusEnum receivedStatusEnum = EnumUtil.getByCode(refundGoods.getReceivedStatus().intValue(), ReceivedStatusEnum.class);
            if (receivedStatusEnum != null) {
                refundGoodsInfoResponse.setReceivedStatusStr(receivedStatusEnum.getDesc());
            }
        } else {
            refundGoodsInfoResponse.setReceivedStatusStr("");
        }
        //根据商品ID--sku查询商品主图【商品服务提供接口】
        List<Long> skuIds = new ArrayList<>();
        skuIds.add(refundGoods.getRefundGoodsId().longValue());
        List<GoodsSkuInfoDTO> getGoodsSkuInfoByIds = goodsClient.getGoodsSkuInfoByIds(skuIds);
        //根据商品skuId获取商家收货地址、商家收货姓名、商家收货手机号
        String storeReceivedAddress = "";
        String storeReceivedName = "";
        String storeReceivedPhone = "";
        if (getGoodsSkuInfoByIds.size() > 0) {
            if (getGoodsSkuInfoByIds.get(0) != null) {
                if (!"".equals(getGoodsSkuInfoByIds.get(0).getReturnAddress())) {
                    storeReceivedAddress = getGoodsSkuInfoByIds.get(0).getReturnAddress();
                }
                if (!"".equals(getGoodsSkuInfoByIds.get(0).getAddressee())) {
                    storeReceivedName = getGoodsSkuInfoByIds.get(0).getAddressee();
                }
                if (!"".equals(getGoodsSkuInfoByIds.get(0).getMobile())) {
                    storeReceivedPhone = getGoodsSkuInfoByIds.get(0).getMobile();
                }
            }
        }
        refundGoodsInfoResponse.setStoreReceivedAddress(storeReceivedAddress);
        refundGoodsInfoResponse.setStoreReceivedName(storeReceivedName);
        refundGoodsInfoResponse.setStoreReceivedPhone(storeReceivedPhone);
        String refundDesc = !"".equals(refundGoods.getRefundDesc()) && refundGoods.getRefundDesc() != null ? refundGoods.getRefundDesc() : "";
        String goodsName = !"".equals(refundGoods.getGoodsName()) && refundGoods.getGoodsName() != null ? refundGoods.getGoodsName() : "";
        String logisticsNo = !"".equals(refundGoods.getLogisticsNo()) && refundGoods.getLogisticsNo() != null ? refundGoods.getLogisticsNo() : "";
        String logisticsCompany = !"".equals(refundGoods.getLogisticsCompany()) && refundGoods.getLogisticsCompany() != null ? refundGoods.getLogisticsCompany() : "";
        String dealWithRemark = !"".equals(refundGoods.getDealWithRemark()) && refundGoods.getDealWithRemark() != null ? refundGoods.getDealWithRemark() : "";
        String receivedRemark = !"".equals(refundGoods.getReceivedRemark()) && refundGoods.getReceivedRemark() != null ? refundGoods.getReceivedRemark() : "";
        String checkRemark = !"".equals(refundGoods.getCheckRemark()) && refundGoods.getCheckRemark() != null ? refundGoods.getCheckRemark() : "";
        refundGoodsInfoResponse.setRefundDesc(refundDesc);
        refundGoodsInfoResponse.setGoodsName(goodsName);
        refundGoodsInfoResponse.setLogisticsNo(logisticsNo);
        refundGoodsInfoResponse.setLogisticsCompany(logisticsCompany);
        refundGoodsInfoResponse.setDealWithRemark(dealWithRemark);
        refundGoodsInfoResponse.setReceivedRemark(receivedRemark);
        refundGoodsInfoResponse.setCheckRemark(checkRemark);
        /*
         * 申请已提交，平台处理中、平台已同意退货，请及时退货、您已发货，等待平台确认收货、退货退款成功/退款成功、已拒绝、申请已撤销
         * 1、申请已提交 【0-待处理】
         * 2、待用户发货 【1-待收货】
         * 3、平台处理中 【1-待收货】
         * 4、退货退款成功/退款成功 【3-已完成】
         * 5、申请已拒绝 【4-已拒绝】
         * 6、申请已撤销 【5-已取消】
         * 退货状态：0-待处理 1-待收货 3-已完成 4-已拒绝 5-已取消
         * 售后类型: 0-退货退款 1-退款
         */
        String statusStr = "";
        if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_DEAL_WITH.getCode().byteValue()) {
            statusStr = FrontRefundStatus.STATUS_DETAIL_APPLY;
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_RECEIVED.getCode().byteValue()) {
            if ("".equals(refundGoods.getLogisticsNo()) || null == refundGoods.getLogisticsNo()) {
                statusStr = FrontRefundStatus.STATUS_DETAIL_WAIT;
            } else {
                statusStr = FrontRefundStatus.STATUS_DETAIL_DEAL_WITH;
            }
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CHECK.getCode().byteValue()) {
            statusStr = FrontRefundStatus.STATUS_DETAIL_APPLY;
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_COMPLETE.getCode().byteValue()) {
            if (refundGoods.getRefundType() == RefundTypeEnum.REFUND_TYPE_GOODS.getCode().byteValue()) {
                statusStr = FrontRefundStatus.STATUS_DETAIL_GOODS_SUCCESS;
            } else {
                statusStr = FrontRefundStatus.STATUS_DETAIL_SUCCESS;
            }
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_REFUSE.getCode().byteValue()) {
            statusStr = FrontRefundStatus.STATUS_DETAIL_REFUSE;
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CANCEL.getCode().byteValue()) {
            statusStr = FrontRefundStatus.STATUS_DETAIL_CANCEL;
        }
        refundGoodsInfoResponse.setStatusStr(statusStr);
        /*
         * 倒计时时间
         * 1、用户申请已提交，等待平台审核。时间2天 【0-待处理】
         * 2、平台审核通过，等待用户上传单号。时间为2天 【1-待收货 未上传单号】
         * 3、用户已发货，等待平台收货。时间7天 【1-待收货 已上传单号】
         * 退货状态：0-待处理 1-待收货 2-待审核 3-已完成 4-已拒绝 5-已取消
         * 收货状态: 1-已收到 2-未收到 3-其它
         */
        //当前时间--时间戳
        long nowTime = System.currentTimeMillis();
        //倒计时剩余时间--时间戳
        long ountDownTime = 0;
        if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_DEAL_WITH.getCode().byteValue()) {
            //用户申请已提交，等待平台审核。时间2天，时间戳 【0-待处理】
            long applyTime = refundGoods.getCreateTime().getTime();
            if ((applyTime + Long.parseLong(expireTimeConfig.getDiffTimeStampOne())) > nowTime) {
                ountDownTime = applyTime + Long.parseLong(expireTimeConfig.getDiffTimeStampOne()) - nowTime;
            }
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_RECEIVED.getCode().byteValue()) {
            if ("".equals(refundGoods.getLogisticsNo()) || null == refundGoods.getLogisticsNo()) {
                //平台审核通过，等待用户上传单号。时间为2天，时间戳【1-待收货 未上传单号】
                long dealWithTime = refundGoods.getDealWithTime().getTime();
                if ((dealWithTime + Long.parseLong(expireTimeConfig.getDiffTimeStampTwo())) > nowTime) {
                    ountDownTime = dealWithTime + Long.parseLong(expireTimeConfig.getDiffTimeStampTwo()) - nowTime;
                }
            } else {
                //用户已发货，等待平台收货。时间7天，时间戳 【1-待收货 已上传单号】
                long logisticsTime = refundGoods.getLogisticsTime().getTime();
                if ((logisticsTime + Long.parseLong(expireTimeConfig.getDiffTimeStampThree())) > nowTime) {
                    ountDownTime = logisticsTime + Long.parseLong(expireTimeConfig.getDiffTimeStampThree()) - nowTime;
                }
            }
        } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CHECK.getCode().byteValue()) {
            //用户申请已提交，等待平台审核---->平台处理通过---->平台选择弃货---->等待财务审核，共用时间。时间2天，时间戳 【2-待审核】
            long applyTime = refundGoods.getCreateTime().getTime();
            if ((applyTime + Long.parseLong(expireTimeConfig.getDiffTimeStampOne())) > nowTime) {
                ountDownTime = applyTime + Long.parseLong(expireTimeConfig.getDiffTimeStampOne()) - nowTime;
            }
        }
        refundGoodsInfoResponse.setCountDownTime(ountDownTime);
        /*
         * 退货状态：0-待处理 1-待收货 2-待审核 3-已完成 4-已拒绝 5-已取消
         * 5-已取消 && 收货时间+8天 > 当前时间。显示按钮
         */
        //默认不显示
        byte buttonFlag = 0;
        if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CANCEL.getCode().byteValue()
                || refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_REFUSE.getCode().byteValue()) {
            Date confirmTime = iOrderService.getConfirmTime(refundGoods.getOriginalOrderId());
            if (confirmTime != null) {
                //收货时间+8天
                long plusEightTime = DateUtil.getDateAfterDay(confirmTime, 8).getTime();
                if (plusEightTime > nowTime) {
                    //收货时间+8天 > 当前时间
                    /*
                     * 查询是否是最新一条数据
                     */
                    QueryWrapper<RefundGoods> queryWrapperR = new QueryWrapper<>();
                    queryWrapperR.eq("del_flag", DelFlag.NORMAL);
                    queryWrapperR.eq("buyer_id", refundGoodsRequest.getUid());
                    queryWrapperR.eq("original_order_id", refundGoods.getOriginalOrderId());
                    queryWrapperR.eq("refund_goods_id", refundGoods.getRefundGoodsId());
                    queryWrapperR.eq("buyer_id", refundGoodsRequest.getUid());
                    queryWrapperR.eq("refund_type", RefundTypeEnum.REFUND_TYPE_GOODS.getCode());
                    queryWrapperR.orderByDesc("id");
                    queryWrapperR.last("limit 1");
                    RefundGoods refundGoodsOne = refundGoodsMapper.selectOne(queryWrapperR);
                    if (refundGoodsOne != null) {
                        if (refundGoodsOne.getId().intValue() == refundGoods.getId().intValue()) {
                            buttonFlag = 1;
                        }
                    }
                }
            }
        }
        refundGoodsInfoResponse.setButtonFlag(buttonFlag);
        refundGoodsInfoResponse.setIsGroup(refundGoods.getIsGroup());
        return refundGoodsInfoResponse;
    }

    /**
     * 售后服务--全部列表
     * 退款/退货退款--未删除的全部信息
     */
    public TableDataInfo<RefundListResponse> refundList(RefundListRequest request) {
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.eq("buyer_id", request.getUid());
        queryWrapper.orderByDesc("id");
        IPage<RefundGoods> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = refundGoodsMapper.selectPage(page, queryWrapper);
        List<RefundGoods> refundGoodss = page.getRecords();
        List<RefundListResponse> result = new ArrayList<>();
        if (refundGoodss.size() > 0) {
            for (RefundGoods refundGoods : refundGoodss) {
                /*************************自动取消************************/
                RefundGoodsAutoCancelRequest refundGoodsAutoCancelRequest = new RefundGoodsAutoCancelRequest();
                refundGoodsAutoCancelRequest.setStatus(refundGoods.getStatus());
                refundGoodsAutoCancelRequest.setUid(refundGoods.getBuyerId());
                refundGoodsAutoCancelRequest.setCreateTime(refundGoods.getCreateTime());
                refundGoodsAutoCancelRequest.setDealWithTime(refundGoods.getDealWithTime());
                refundGoodsAutoCancelRequest.setLogisticsNo(refundGoods.getLogisticsNo());
                refundGoodsAutoCancelRequest.setLogisticsTime(refundGoods.getLogisticsTime());
                refundGoodsAutoCancelRequest.setRefundType(refundGoods.getRefundType());
                refundGoodsAutoCancelRequest.setRefundId(refundGoods.getId());
                refundGoodsAutoCancelRequest.setOrderId(refundGoods.getOriginalOrderId());
                refundGoodsAutoCancelRequest.setGoodsId(refundGoods.getRefundGoodsId());
                refundGoodsCommonService.autoCancel(refundGoodsAutoCancelRequest);
                /*************************自动取消************************/
                RefundListResponse refundListResponse = new RefundListResponse();
                refundListResponse.setRefundId(refundGoods.getId());
                String goodsName = !"".equals(refundGoods.getGoodsName()) && refundGoods.getGoodsName() != null ? refundGoods.getGoodsName() : "";
                String goodsSpec = !"".equals(refundGoods.getGoodsSpec()) && refundGoods.getGoodsSpec() != null ? refundGoods.getGoodsSpec() : "";
                String goodsPic = !"".equals(refundGoods.getGoodsPic()) && refundGoods.getGoodsPic() != null ? refundGoods.getGoodsPic() : "";
                refundListResponse.setGoodsName(goodsName);
                refundListResponse.setGoodsSpec(goodsSpec);
                refundListResponse.setGoodsPic(ImageResizeUtil.imageResize(goodsPic, ImageResizeEnum.LIST));
                /*
                 * 申请已提交、待用户发货、平台处理中、退货退款成功/退款成功、申请已拒绝、申请已撤销
                 * 1、申请已提交 【0-待处理】
                 * 2、待用户发货 【1-待收货】
                 * 3、平台处理中 【1-待收货】
                 * 4、退货退款成功/退款成功 【3-已完成】
                 * 5、申请已拒绝 【4-已拒绝】
                 * 6、申请已撤销 【5-已取消】
                 * 退货状态：0-待处理 1-待收货 3-已完成 4-已拒绝 5-已取消
                 * 售后类型: 0-退货退款 1-退款
                 */
                String statusStr = "";
                if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_DEAL_WITH.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_APPLY;
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_RECEIVED.getCode().byteValue()) {
                    if ("".equals(refundGoods.getLogisticsNo()) || null == refundGoods.getLogisticsNo()) {
                        statusStr = FrontRefundStatus.STATUS_WAIT;
                    } else {
                        statusStr = FrontRefundStatus.STATUS_DEAL_WITH;
                    }
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CHECK.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_APPLY;
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_COMPLETE.getCode().byteValue()) {
                    if (refundGoods.getRefundType() == RefundTypeEnum.REFUND_TYPE_GOODS.getCode().byteValue()) {
                        statusStr = FrontRefundStatus.STATUS_GOODS_SUCCESS;
                    } else {
                        statusStr = FrontRefundStatus.STATUS_SUCCESS;
                    }
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_REFUSE.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_REFUSE;
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CANCEL.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_CANCEL;
                }
                refundListResponse.setStatusStr(statusStr);
                result.add(refundListResponse);
            }
        }
        TableDataInfo<RefundListResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 售后服务--待处理列表
     * 退货退款--未删除--待收货
     */
    public TableDataInfo<RefundListResponse> refundWaitList(RefundListRequest request) {
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.eq("refund_type", RefundTypeEnum.REFUND_TYPE_GOODS.getCode());
        queryWrapper.eq("status", RefundGoodsStatusEnum.STATUS_RECEIVED.getCode());
        queryWrapper.isNull("logistics_no");
        queryWrapper.eq("buyer_id", request.getUid());
        queryWrapper.orderByDesc("id");
        IPage<RefundGoods> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = refundGoodsMapper.selectPage(page, queryWrapper);
        List<RefundGoods> refundGoodss = page.getRecords();
        List<RefundListResponse> result = new ArrayList<>();
        if (refundGoodss.size() > 0) {
            for (RefundGoods refundGoods : refundGoodss) {
                /*************************自动取消************************/
                RefundGoodsAutoCancelRequest refundGoodsAutoCancelRequest = new RefundGoodsAutoCancelRequest();
                refundGoodsAutoCancelRequest.setStatus(refundGoods.getStatus());
                refundGoodsAutoCancelRequest.setUid(refundGoods.getBuyerId());
                refundGoodsAutoCancelRequest.setCreateTime(refundGoods.getCreateTime());
                refundGoodsAutoCancelRequest.setDealWithTime(refundGoods.getDealWithTime());
                refundGoodsAutoCancelRequest.setLogisticsNo(refundGoods.getLogisticsNo());
                refundGoodsAutoCancelRequest.setLogisticsTime(refundGoods.getLogisticsTime());
                refundGoodsAutoCancelRequest.setRefundType(refundGoods.getRefundType());
                refundGoodsAutoCancelRequest.setRefundId(refundGoods.getId());
                refundGoodsAutoCancelRequest.setOrderId(refundGoods.getOriginalOrderId());
                refundGoodsAutoCancelRequest.setGoodsId(refundGoods.getRefundGoodsId());
                refundGoodsCommonService.autoCancel(refundGoodsAutoCancelRequest);
                /*************************自动取消************************/
                RefundListResponse refundListResponse = new RefundListResponse();
                refundListResponse.setRefundId(refundGoods.getId());
                String goodsName = !"".equals(refundGoods.getGoodsName()) && refundGoods.getGoodsName() != null ? refundGoods.getGoodsName() : "";
                String goodsSpec = !"".equals(refundGoods.getGoodsSpec()) && refundGoods.getGoodsSpec() != null ? refundGoods.getGoodsSpec() : "";
                String goodsPic = !"".equals(refundGoods.getGoodsPic()) && refundGoods.getGoodsPic() != null ? refundGoods.getGoodsPic() : "";
                refundListResponse.setGoodsName(goodsName);
                refundListResponse.setGoodsSpec(goodsSpec);
                refundListResponse.setGoodsPic(ImageResizeUtil.imageResize(goodsPic, ImageResizeEnum.LIST));
                /*
                 * 申请已提交、待用户发货、平台处理中、退货退款成功/退款成功、申请已拒绝、申请已撤销
                 * 1、申请已提交 【0-待处理】
                 * 2、待用户发货 【1-待收货】
                 * 3、平台处理中 【1-待收货】
                 * 4、退货退款成功/退款成功 【3-已完成】
                 * 5、申请已拒绝 【4-已拒绝】
                 * 6、申请已撤销 【5-已取消】
                 * 退货状态：0-待处理 1-待收货 3-已完成 4-已拒绝 5-已取消
                 * 售后类型: 0-退货退款 1-退款
                 */
                String statusStr = "";
                if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_DEAL_WITH.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_APPLY;
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_RECEIVED.getCode().byteValue()) {
                    if ("".equals(refundGoods.getLogisticsNo()) || null == refundGoods.getLogisticsNo()) {
                        statusStr = FrontRefundStatus.STATUS_WAIT;
                    } else {
                        statusStr = FrontRefundStatus.STATUS_DEAL_WITH;
                    }
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CHECK.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_APPLY;
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_COMPLETE.getCode().byteValue()) {
                    if (refundGoods.getRefundType() == RefundTypeEnum.REFUND_TYPE_GOODS.getCode().byteValue()) {
                        statusStr = FrontRefundStatus.STATUS_GOODS_SUCCESS;
                    } else {
                        statusStr = FrontRefundStatus.STATUS_SUCCESS;
                    }
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_REFUSE.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_REFUSE;
                } else if (refundGoods.getStatus() == RefundGoodsStatusEnum.STATUS_CANCEL.getCode().byteValue()) {
                    statusStr = FrontRefundStatus.STATUS_CANCEL;
                }
                refundListResponse.setStatusStr(statusStr);
                result.add(refundListResponse);
            }
        }
        TableDataInfo<RefundListResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 售后订单列表
     */
    public List<RefundListByOrderResponse> refundOrderList(RefundListByOrderRequest request) {
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.eq("buyer_id", request.getUid());
        queryWrapper.eq("original_order_id", request.getOrderId());
        queryWrapper.orderByDesc("id");
        List<RefundGoods> refundGoodss = refundGoodsMapper.selectList(queryWrapper);
        List<RefundListByOrderResponse> result = new ArrayList<>();
        if (refundGoodss.size() > 0) {
            for (RefundGoods refundGoods : refundGoodss) {
                /*************************自动取消************************/
                RefundGoodsAutoCancelRequest refundGoodsAutoCancelRequest = new RefundGoodsAutoCancelRequest();
                refundGoodsAutoCancelRequest.setStatus(refundGoods.getStatus());
                refundGoodsAutoCancelRequest.setUid(refundGoods.getBuyerId());
                refundGoodsAutoCancelRequest.setCreateTime(refundGoods.getCreateTime());
                refundGoodsAutoCancelRequest.setDealWithTime(refundGoods.getDealWithTime());
                refundGoodsAutoCancelRequest.setLogisticsNo(refundGoods.getLogisticsNo());
                refundGoodsAutoCancelRequest.setLogisticsTime(refundGoods.getLogisticsTime());
                refundGoodsAutoCancelRequest.setRefundType(refundGoods.getRefundType());
                refundGoodsAutoCancelRequest.setRefundId(refundGoods.getId());
                refundGoodsAutoCancelRequest.setOrderId(refundGoods.getOriginalOrderId());
                refundGoodsAutoCancelRequest.setGoodsId(refundGoods.getRefundGoodsId());
                refundGoodsCommonService.autoCancel(refundGoodsAutoCancelRequest);
                /*************************自动取消************************/
                RefundListByOrderResponse refundListByOrderResponse = new RefundListByOrderResponse();
                refundListByOrderResponse.setRefundId(refundGoods.getId());
                String goodsName = !"".equals(refundGoods.getGoodsName()) && refundGoods.getGoodsName() != null ? refundGoods.getGoodsName() : "";
                String goodsSpec = !"".equals(refundGoods.getGoodsSpec()) && refundGoods.getGoodsSpec() != null ? refundGoods.getGoodsSpec() : "";
                String goodsPic = !"".equals(refundGoods.getGoodsPic()) && refundGoods.getGoodsPic() != null ? refundGoods.getGoodsPic() : "";
                refundListByOrderResponse.setGoodsName(goodsName);
                refundListByOrderResponse.setGoodsSpec(goodsSpec);
                refundListByOrderResponse.setGoodsPic(ImageResizeUtil.imageResize(goodsPic, ImageResizeEnum.LIST));
                refundListByOrderResponse.setRefundNum(refundGoods.getRefundNum());
                result.add(refundListByOrderResponse);
            }
        } else {
            throw new BaseException(RefundGoodsExceptionEnum.DEL_FLAG_YES.getCode(), RefundGoodsExceptionEnum.DEL_FLAG_YES.getMessage());
        }
        return result;
    }

    /**
     * 退货状态--对照字典
     */
    public List<AppTypeListResponse> getRefundGoodsStatus() {
        List<AppTypeListResponse> result = new ArrayList<>();
        AppTypeListResponse appTypeListResponse1 = new AppTypeListResponse();
        appTypeListResponse1.setTypeId(RefundGoodsStatusEnum.STATUS_DEAL_WITH.getCode());
        appTypeListResponse1.setTypeName(RefundGoodsStatusEnum.STATUS_DEAL_WITH.getDesc());
        result.add(appTypeListResponse1);
        AppTypeListResponse appTypeListResponse2 = new AppTypeListResponse();
        appTypeListResponse2.setTypeId(RefundGoodsStatusEnum.STATUS_RECEIVED.getCode());
        appTypeListResponse2.setTypeName(RefundGoodsStatusEnum.STATUS_RECEIVED.getDesc());
        result.add(appTypeListResponse2);
        AppTypeListResponse appTypeListResponse3 = new AppTypeListResponse();
        appTypeListResponse3.setTypeId(RefundGoodsStatusEnum.STATUS_CHECK.getCode());
        appTypeListResponse3.setTypeName(RefundGoodsStatusEnum.STATUS_CHECK.getDesc());
        result.add(appTypeListResponse3);
        AppTypeListResponse appTypeListResponse4 = new AppTypeListResponse();
        appTypeListResponse4.setTypeId(RefundGoodsStatusEnum.STATUS_COMPLETE.getCode());
        appTypeListResponse4.setTypeName(RefundGoodsStatusEnum.STATUS_COMPLETE.getDesc());
        result.add(appTypeListResponse4);
        AppTypeListResponse appTypeListResponse5 = new AppTypeListResponse();
        appTypeListResponse5.setTypeId(RefundGoodsStatusEnum.STATUS_REFUSE.getCode());
        appTypeListResponse5.setTypeName(RefundGoodsStatusEnum.STATUS_REFUSE.getDesc());
        result.add(appTypeListResponse5);
        AppTypeListResponse appTypeListResponse6 = new AppTypeListResponse();
        appTypeListResponse6.setTypeId(RefundGoodsStatusEnum.STATUS_CANCEL.getCode());
        appTypeListResponse6.setTypeName(RefundGoodsStatusEnum.STATUS_CANCEL.getDesc());
        result.add(appTypeListResponse6);
        return result;
    }

    /**
     * 售后原因--对照字典
     */
    public List<AppTypeListResponse> getAfterSaleReason() {
        List<AppTypeListResponse> result = new ArrayList<>();
        AppTypeListResponse appTypeListResponse1 = new AppTypeListResponse();
        appTypeListResponse1.setTypeId(AfterSaleReasonEnum.AFTER_SALE_REASON_LOSE.getCode());
        appTypeListResponse1.setTypeName(AfterSaleReasonEnum.AFTER_SALE_REASON_LOSE.getDesc());
        result.add(appTypeListResponse1);
        AppTypeListResponse appTypeListResponse2 = new AppTypeListResponse();
        appTypeListResponse2.setTypeId(AfterSaleReasonEnum.AFTER_SALE_REASON_DAMAGED.getCode());
        appTypeListResponse2.setTypeName(AfterSaleReasonEnum.AFTER_SALE_REASON_DAMAGED.getDesc());
        result.add(appTypeListResponse2);
        AppTypeListResponse appTypeListResponse3 = new AppTypeListResponse();
        appTypeListResponse3.setTypeId(AfterSaleReasonEnum.AFTER_SALE_REASON_MISSED.getCode());
        appTypeListResponse3.setTypeName(AfterSaleReasonEnum.AFTER_SALE_REASON_MISSED.getDesc());
        result.add(appTypeListResponse3);
        AppTypeListResponse appTypeListResponse4 = new AppTypeListResponse();
        appTypeListResponse4.setTypeId(AfterSaleReasonEnum.AFTER_SALE_REASON_OTHER.getCode());
        appTypeListResponse4.setTypeName(AfterSaleReasonEnum.AFTER_SALE_REASON_OTHER.getDesc());
        result.add(appTypeListResponse4);
        return result;
    }

    /**
     * 退货原因--对照字典
     */
    public List<AppTypeListResponse> getRefundReason() {
        List<AppTypeListResponse> result = new ArrayList<>();
        AppTypeListResponse appTypeListResponse1 = new AppTypeListResponse();
        appTypeListResponse1.setTypeId(RefundReasonEnum.REFUND_REASON_REPEAT.getCode());
        appTypeListResponse1.setTypeName(RefundReasonEnum.REFUND_REASON_REPEAT.getDesc());
        result.add(appTypeListResponse1);
        AppTypeListResponse appTypeListResponse2 = new AppTypeListResponse();
        appTypeListResponse2.setTypeId(RefundReasonEnum.REFUND_REASON_NOLIKE.getCode());
        appTypeListResponse2.setTypeName(RefundReasonEnum.REFUND_REASON_NOLIKE.getDesc());
        result.add(appTypeListResponse2);
        AppTypeListResponse appTypeListResponse3 = new AppTypeListResponse();
        appTypeListResponse3.setTypeId(RefundReasonEnum.REFUND_REASON_NOMATCH.getCode());
        appTypeListResponse3.setTypeName(RefundReasonEnum.REFUND_REASON_NOMATCH.getDesc());
        result.add(appTypeListResponse3);
        AppTypeListResponse appTypeListResponse4 = new AppTypeListResponse();
        appTypeListResponse4.setTypeId(RefundReasonEnum.REFUND_REASON_OVERDUE.getCode());
        appTypeListResponse4.setTypeName(RefundReasonEnum.REFUND_REASON_OVERDUE.getDesc());
        result.add(appTypeListResponse4);
        AppTypeListResponse appTypeListResponse5 = new AppTypeListResponse();
        appTypeListResponse5.setTypeId(RefundReasonEnum.REFUND_REASON_QUALITY.getCode());
        appTypeListResponse5.setTypeName(RefundReasonEnum.REFUND_REASON_QUALITY.getDesc());
        result.add(appTypeListResponse5);
        AppTypeListResponse appTypeListResponse6 = new AppTypeListResponse();
        appTypeListResponse6.setTypeId(RefundReasonEnum.REFUND_REASON_LESS.getCode());
        appTypeListResponse6.setTypeName(RefundReasonEnum.REFUND_REASON_LESS.getDesc());
        result.add(appTypeListResponse6);
        return result;
    }

    /**
     * 退款原因--对照字典
     */
    public List<AppTypeListResponse> getRefundMoneyReason() {
        List<AppTypeListResponse> result = new ArrayList<>();
        AppTypeListResponse appTypeListResponse1 = new AppTypeListResponse();
        appTypeListResponse1.setTypeId(RefundMoneyReasonEnum.REFUND_MONEY_REASON_REPEAT.getCode());
        appTypeListResponse1.setTypeName(RefundMoneyReasonEnum.REFUND_MONEY_REASON_REPEAT.getDesc());
        result.add(appTypeListResponse1);
        AppTypeListResponse appTypeListResponse2 = new AppTypeListResponse();
        appTypeListResponse2.setTypeId(RefundMoneyReasonEnum.REFUND_MONEY_REASON_NOLIKE.getCode());
        appTypeListResponse2.setTypeName(RefundMoneyReasonEnum.REFUND_MONEY_REASON_NOLIKE.getDesc());
        result.add(appTypeListResponse2);
        AppTypeListResponse appTypeListResponse3 = new AppTypeListResponse();
        appTypeListResponse3.setTypeId(RefundMoneyReasonEnum.REFUND_MONEY_REASON_NOTSHIPPED.getCode());
        appTypeListResponse3.setTypeName(RefundMoneyReasonEnum.REFUND_MONEY_REASON_NOTSHIPPED.getDesc());
        result.add(appTypeListResponse3);
        AppTypeListResponse appTypeListResponse4 = new AppTypeListResponse();
        appTypeListResponse4.setTypeId(RefundMoneyReasonEnum.REFUND_MONEY_REASON_NOGOODS.getCode());
        appTypeListResponse4.setTypeName(RefundMoneyReasonEnum.REFUND_MONEY_REASON_NOGOODS.getDesc());
        result.add(appTypeListResponse4);
        AppTypeListResponse appTypeListResponse5 = new AppTypeListResponse();
        appTypeListResponse5.setTypeId(RefundMoneyReasonEnum.REFUND_MONEY_REASON_OTHER.getCode());
        appTypeListResponse5.setTypeName(RefundMoneyReasonEnum.REFUND_MONEY_REASON_OTHER.getDesc());
        result.add(appTypeListResponse5);
        return result;
    }

    /**
     * 生成订单号
     */
    private String generateOrderSn() {
        String time = DateUtil.getAllMSTime().substring(2);
        int randomInt = ThreadLocalRandom.current().nextInt(10, 100);
        return "TH" + time + randomInt;
    }

}
